<?php
class lib_sys_user extends lib_base{
    public $table_name = "qc_sys_user";
    public $pk = "id";

    public function up_userinfo($user_id,$userinfo) 
    {   
        // $nickName = filterEmoji($userinfo['name']);
        $nickName = $userinfo['nickName'];
        $arrBase = array(
            'nickName'=>$nickName,
            // 'gender'=>$userinfo['gender'],
            // 'city'=>$userinfo['city'],
            // 'province'=>$userinfo['province'],
            // 'country'=>$userinfo['country'],
            'avatarUrl'=>$userinfo['avatarUrl'],
            'up_time'=>date('Y-m-d H:i:s'),
        );
        $res = $this->edit($user_id,$arrBase);
        return $res;
    }

    public function tel_login($tel) 
    {   
        // 查询用户是否已存在
        $user = $this->find(array("tel='{$tel}'"));
        if(!$user){
            $arrBase = array(
                // 'site_id'=>$data['wxapp_id'],
                // 'company_id'=>$post['wxapp_id'],
                'tel'=>$tel,
                'status'=>1,
                'add_time' => date('Y-m-d H:i:s'),
            );
            $user_id = $this->create($arrBase);
            $user_tel = $tel;
        }else{
            $arrBase = array(
                'up_time'=>date('Y-m-d H:i:s'),
            );
            $this->edit($user_id,$arrBase);

            $user_id = $user['id'];
            $user_tel = $user['tel'];
        }
        $this->token = pp_encrypt($user_id);
        // 记录推荐人关系
        // if (!$user && $refereeId > 0) {
        //     RefereeModel::createRelation($model['user_id'], $refereeId);
        // }

        $data['user_id'] = $user_id;
        $data['user_tel'] = $user_tel;
        $data['user_token'] = $this->token;
        return $data;
    }

    public function login($data,$config) 
    {   
        $this->config = $config;
        // 微信登录 获取session_key
        $session = D('weixin')->wxlogin($data['code'],$config);
        // 自动注册用户
        $refereeId = isset($data['referee_id']) ? $data['referee_id'] : null;
        // $userInfo = json_decode(htmlspecialchars_decode($post['userinfo']), true);
        $user = $this->register($session['openid'], $data, $refereeId);
        // 生成token (session3rd)
        // $this->token = $this->set_token($session['openid']);
        // $this->token = pp_encrypt($session['openid']);
        // 记录缓存, 7天
        // Cache::set($this->token, $session, 86400 * 7);

        // $res['user_id'] = $user_id;
        // $res['token'] = $this->token;
        
        // $this->up_time($user['user_id']);
        return $user;
    }

    public function register($open_id, $data, $refereeId = null)
    {
        $userinfo = $data['userinfo'];
        $nickName = filterEmoji($userinfo['nickName']);
        $wxapp_id = $data['wxapp_id'];
        // 查询用户是否已存在
        $user = $this->find(array("tel='{$data['user_tel']}'"));
        if($user){
            $user_id = $user['id'];
            $arrBase = array(
                'open_id'=>$open_id,
                'nickName'=>$nickName,
                'gender'=>$userinfo['gender'],
                'city'=>$userinfo['city'],
                'province'=>$userinfo['province'],
                'country'=>$userinfo['country'],
                'avatarUrl'=>$userinfo['avatarUrl'],
                'up_time'=>date('Y-m-d H:i:s'),
            );
            $this->edit($user_id,$arrBase);
        }

        // 记录推荐人关系
        // if (!$user && $refereeId > 0) {
        //     RefereeModel::createRelation($model['user_id'], $refereeId);
        // }

        $res['user_id'] = $user_id;
        // $res['user_token'] = $this->get_token();
        // $data['user_tel'] = $user['tel'];
        return $res;
    }

    public function check_login($username,$password){
        $cond = array("username=:c1 and password=:c2 and status=1", 
            ":c1" => $username,
            ":c2" => qcing_hash($password),
        );
        return $this->find($cond);
    }

    public function up_time($id){
       $arrBase = array(
            'up_time'=>date('Y-m-d H:i:s'),
            // 'last_ip'=>get_ip(),
        );
        return $this->edit($id,$arrBase);
    }

    public function set_token($open_id)
    {
        // 当前时间戳 (精确到毫秒)
        $timeStamp = microtime(true);
        // 自定义一个盐
        $salt = 'token_salt';
        return md5("{$open_id}_{$timeStamp}_{$salt}");
    }
    
    public function get_token()
    {
        return $this->token;
    }

    public function get_user($token)
    {
        $open_id = pp_encrypt($token,'DECODE');
        $user = $this->find(array("id = '{$open_id}'"));
        return $user;
    }


    public function checkText($config,$content) 
    {   
        $this->config = $config;
        $WxUser = new \lib\Weixin($this->config);
        $result = $WxUser->checkText($content);
        return $result;
    }

    public function getQrcode($config,$data) 
    {   
        $this->config = $config;
        $WxUser = new \lib\Weixin($this->config);
        $result = $WxUser->getQrcode($data);
        return $result;
    }

    public function getUserTel($config,$code,$iv,$encryptedData){
        $this->config = $config;
        $WxUser = new \lib\Weixin($this->config);
        $result = $WxUser->getTel($code,$iv,$encryptedData);
        return $result;
    }
    
}